package com.example.freemaker.freemaker.com.example.freemaker.freemaker.handler;

import com.itextpdf.text.DocumentException;
import com.itextpdf.tool.xml.exceptions.CssResolverException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: desk-----xuzhiliang
 * @Date: 2019/8/9 14:50
 * @Description:
 */
@Controller
public class TestHandler {
    TestHandler(){
        System.out.println("init");
    }
    @Autowired
    FreeMakerTamplter freeMakerTamplter;
    @Autowired
    HtmlToXHtml htmlToXHtml;
    @Autowired
    XHtmlToPdf xHtmlToPdf;
    @RequestMapping("/test")
    public ModelAndView test(){
        System.out.println("something running");
        ModelAndView modelAndView = new ModelAndView("/demo");
        modelAndView.addObject("name","hah--------->");
        List list = new ArrayList();
        list.add("张国荣");
        list.add("周杰伦");
        list.add("张惠妹");
        modelAndView.addObject("singers",list);
        modelAndView.addObject("isTrue",false);
        return modelAndView;
    }
    @RequestMapping("/download")
    //freemaker的模板的转化和二进制文件的下载的实现
    public ResponseEntity<byte[]> download(HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse){
        //尝试向freemaker文件传值
        Map map = new HashMap();
        map.put("name","傻逼");
        String filename = "傻逼.html";
        //生成模板文件
        //可以直接返回指定write流
        freeMakerTamplter.freemarkToHtml("src\\main\\resources\\templates\\","fileToPDF.ftl",map,"src\\main\\resources\\temp\\"+filename);
        //html转化到xhtml
        htmlToXHtml.html2Xhtml("src\\main\\resources\\temp\\"+filename,"src\\main\\resources\\temp\\"+"转化之后的文件.html");
        try {
            xHtmlToPdf.XHtml2Pdf("src\\main\\resources\\temp\\"+"转化之后的文件.html","src\\main\\resources\\temp\\"+"转化之后的文件.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (CssResolverException e) {
            e.printStackTrace();
        }

        //读取模板文件------》执行下载
        File file = new File("src\\main\\resources\\temp\\"+"转化之后的文件.pdf");
        byte[] body = null;
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            if (is.available()==0){
                return null;
            }
            body = new byte[is.available()];
            is.read(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        httpServletResponse.setContentType("application/octet-stream");
        try {
            httpServletRequest.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return entity;
    }
}
